package com.yanwq.sayhi.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yanwq.sayhi.bean.Article;
import com.yanwq.sayhi.bean.ArticleAttitude;
import com.yanwq.sayhi.bean.Club;
import com.yanwq.sayhi.bean.User;
import com.yanwq.sayhi.repository.ArticleAttitudeRepo;
import com.yanwq.sayhi.repository.ArticleRepo;
import com.yanwq.sayhi.repository.CommentRepo;
import com.yanwq.sayhi.repository.UserRepo;
import com.yanwq.sayhi.rest.UserRest;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：yanweiqiang。
 * 日期：2016/2/2。
 */
@Service
@Transactional
public class ArticleService {
    @Autowired
    private ArticleRepo repo;
    @Autowired
    private CommentRepo commentRepo;
    @Autowired
    private ArticleAttitudeRepo articleAttitudeRepo;
    @Autowired
    private UserRepo userRepo;

    public Article save(Article article) {
        Article ret = repo.save(article);
        ret.setCommentCount(commentRepo.countByArticle(ret));
        return ret;
    }

    public int countByPublishTime(long publishTime) {
        return repo.countByPublishTimeGreaterThanAndPermission(publishTime, 0);
    }

    public int countByPublishTimeAndClubId(long publishTime, long clubId) {
        Club club = new Club();
        club.setId(clubId);
        return repo.countByPublishTimeGreaterThanAndClub(publishTime, club);
    }

    public JSONArray listClubNewArticleCount(long refreshTime, List<Long> clubIds) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<Long, Integer> map = new HashMap<>();
        for (Long clubId : clubIds) {
            Club club = new Club();
            club.setId(clubId);
            map.put(clubId, repo.countByPublishTimeGreaterThanAndClub(refreshTime, club));
        }

        try {
            return JSONArray.fromObject(objectMapper.writeValueAsString(map));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return new JSONArray();
    }

    public List<Article> listNew(long id, int page, int size) {
        Page<Article> articles = repo.findByPermissionAndIdGreaterThan(0, id, new PageRequest(page, size));
        return articles.getContent();
    }

    public List<Article> listOld(long id, int page, int size) {
        Page<Article> articles = repo.findByPermissionAndIdLessThan(0, id, new PageRequest(page, size));
        return articles.getContent();
    }

    private List<Article> setAttitudeAndComment(List<Article> articles, String token) {
        User user = userRepo.findByToken(token);

        for (Article ret : articles) {
            ret.setCommentCount(commentRepo.countByArticle(ret));

            ret.setAttitude0Count(articleAttitudeRepo.countByArticleAndType(ret, 0));
            ret.setAttitude1Count(articleAttitudeRepo.countByArticleAndType(ret, 1));
            ret.setAttitude(articleAttitudeRepo.findByArticleAndUser(ret, user));
        }

        return articles;
    }

    public List<Article> listByPermission(int page, int size, int permission, String token) {
        Page<Article> articles = repo.findByPermissionOrderByPublishTimeDesc(permission, new PageRequest(page, size));
        List<Article> content = articles.getContent();
        return setAttitudeAndComment(content, token);
    }

    public List<Article> listByType(int page, int size, int type, String token) {
        Page<Article> articles = repo.findByTypeOrderByPublishTimeDesc(type, new PageRequest(page, size));
        List<Article> content = articles.getContent();
        return setAttitudeAndComment(content, token);
    }

    public List<Article> listByPermissionAndType(int page, int size, int permission, int type, String token) {
        Page<Article> articles = repo.findByPermissionAndTypeOrderByPublishTimeDesc(permission, type, new PageRequest(page, size));
        List<Article> content = articles.getContent();
        return setAttitudeAndComment(content, token);
    }

    public List<Article> listByClub(int page, int size, long clubId, String token) {
        Club club = new Club();
        club.setId(clubId);
        Page<Article> articles = repo.findByClubOrderByPublishTimeDesc(club, new PageRequest(page, size));
        List<Article> content = articles.getContent();
        return setAttitudeAndComment(content, token);
    }

    public List<Article> listByClubAndType(int page, int size, long clubId, int type, String token) {
        Club club = new Club();
        club.setId(clubId);
        Page<Article> articles = repo.findByClubAndTypeOrderByPublishTimeDesc(club, type, new PageRequest(page, size));
        List<Article> content = articles.getContent();
        return setAttitudeAndComment(content, token);
    }

    public List<Article> listByUser(int page, int size, User user, String token) {
        Page<Article> articles = repo.findByUserOrderByPublishTimeDesc(user, new PageRequest(page, size));
        List<Article> content = articles.getContent();
        return setAttitudeAndComment(content, token);
    }
}